/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-23 10:05:24
 * @LastEditTime: 2019-08-24 10:19:04
 * @LastEditors: Please set LastEditors
 */
/*
 * @lc app=leetcode.cn id=17 lang=javascript
 *
 * [17] 电话号码的字母组合
 *
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/
 *
 * algorithms
 * Medium (50.53%)
 * Likes:    422
 * Dislikes: 0
 * Total Accepted:    38.8K
 * Total Submissions: 76.7K
 * Testcase Example:  '"23"'
 *
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 * 
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 
 * 
 * 
 * 示例:
 * 
 * 输入："23"
 * 输出：["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 * 
 * 
 * 说明:
 * 尽管上面的答案是按字典序排列的，但是你可以任意选择答案输出的顺序。
 * 
 */
/**
 * @param {string} digits
 * @return {string[]}
 */

// 用reduce更快。。。。。。。。。

var letterCombinations = function(digits) {
    const str = {
        2:'abc',
        3:'def',
        4:'ghi',
        5:'jkl',
        6:'mno',
        7:'pqrs',
        8:'tuv',
        9:'wxyz'
    }
    if(!digits) return []
    if(digits.length == 1){
        return str[digits].split('')
    }
    const str_num = []
    digits.split('').forEach(item=>{
        str_num.push(str[item])
    })
    // 递归
    let a = 0
    while(a<str_num.length - 1){
        const result = []
        for(let i=0;i<str_num[a].length;i++){
            for(let j=0;j<str_num[a+1].length;j++){
                result.push(str_num[a][i]+str_num[a+1][j])
            }
        }
        str_num[a+1] = result
        a++
    }
    return str_num.pop()

};

